﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;

namespace VOL.WebApi.Controllers.Finance.tasklist
{
    [Route("api/yjy_whole_info_task")]
    [ApiController]
    public class yjy_whole_info_taskController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("getbomandpriceofrfc"), AllowAnonymous]//产品定价取SAPBOM和价格后台job
        public dynamic getBomAndPriceOfRFC()
        {
            Hashtable hashtable = new Hashtable();
            // string url = "http://10.100.150.18:8181/api/yjy_whole_wlzb/get_whole_sap_bom?P_DATUB_OUT={0}&P_MATNR_OUT={1}";//net6 BOM同步
             string url = "http://10.191.30.35:9991/api/yjy_whole_wlzb/get_whole_sap_bom?P_DATUB_OUT={0}&P_MATNR_OUT={1}";//net6 BOM同步
             string datetime = DateTime.Now.ToString("yyyyMMdd").ToString();
            //初始状态 0 
            int num = 0;
             List<yjy_whole_info> infoList = DBServerProvider.DbContext.Set<yjy_whole_info>().Where(x => x.bom_state == 0 || x.price_state == 0).ToList();
             foreach (var item in infoList)
             {
                if (item.bom_state == 0)
                {
                    string str = string.Format(url, datetime, item.materialnum);
                    var request = (HttpWebRequest)WebRequest.Create(str);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    var response = (HttpWebResponse)request.GetResponse();
                    var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                    if (responseString.Equals("未查询到该物料BOM") || responseString.Equals("物料不存在"))
                    {
                        item.bom_state = 0;
                    }
                    else
                    {
                        item.bom_state = 1;//BOM同步1
                    }
                    hashtable.Add("BOM-" + item.materialnum + "-" + item.yjy_whole_info_id, responseString);
                }
                if (item.price_state==0)
                {
                    // string url2 = "http://10.100.150.18:8181/api/yjy_whole_wlzb/get_whole_sap_bom_price?P_MATNR={0}"; //net6 价格同步
                    string url2 = "http://10.191.30.35:9991/api/yjy_whole_wlzb/get_whole_sap_bom_price?P_MATNR={0}"; //net6 价格同步
                    List<yjy_whole_info> infoList2 = DBServerProvider.DbContext.Set<yjy_whole_info>().Where(x => x.price_state == 0).ToList();
                    string str = string.Format(url2, item.materialnum);
                    var request = (HttpWebRequest)WebRequest.Create(str);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    var response = (HttpWebResponse)request.GetResponse();
                    var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                    if (responseString.ToInt16() == 0)
                    {
                        item.price_state = 0;
                    }
                    else
                    {
                        item.price_state = 1;
                    }
                    hashtable.Add("PRICE-" + item.materialnum + "-" + item.yjy_whole_info_id, responseString);
                }
                DBServerProvider.DbContext.Update(item); 
             }
            DBServerProvider.DbContext.SaveChanges();
            return hashtable;
        }
    }
}
